<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/7/16
 * Time: 20:45
 */

namespace app\api\controller\worker;
use app\api\controller\Base;
use app\common\model\node\NodeModel;
use app\common\model\node\NodeProcessModel;
use app\common\model\worker\CustomerAuthModel;
use app\common\model\worker\CustomerChannelModel;
use app\common\model\worker\CustomerModel;
use app\common\model\worker\RecommendFollowLogModel;
use app\common\model\worker\RecommendModel;
use app\common\model\worker\TaskModel;
use DateTime;
use app\api\controller\worker\TaskController;
use think\Request;

class RecommendFollowLogController extends Base
{
    private $model;
    private $taskModel;
    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
        $this->model = new RecommendFollowLogModel();
        $this->taskModel = new TaskModel();
    }
    public function add(){
//        $roles_ids = $this->role_ids;
//        if(!in_array(6,$roles_ids) ||!in_array(7,$roles_ids) || !in_array(8,$roles_ids)  ){
//            exception('该用户没有权限，只有设计师，业务员，客户代表有操作权限！');
//        }
        //var_dump($this->loginInfo);exit;
        $data = $this->request->param();
        $this->validate($data,[
            'recommend_id|推荐人id'=>'require',
            'follow_type|跟进方式'=>'require',
            'content|沟通内容'=>'require',
            'reply_content|结果'=>'require',
//            'next_follow_time|下次跟进时间'=>'require',
//            'next_content|下次跟进内容'=>'require',
        ]);
        if(!empty($data['follow_imgs'])){
            $data['follow_imgs'] = json_encode($data['follow_imgs'],JSON_UNESCAPED_UNICODE);
        }
        $data['action_admin_id'] = $this->loginInfo['id'];
        //todo 验证是否有编辑权限
        $info = $this->model->add($data);

        $this->result($info,'操作成功',1);
    }

    /**
     * @throws \think\db\exception\DbException
     * @author bxm
     * @date 2022-07-16 20:52
     * 推荐人跟进记录
     */
    public function index(){
        $data = $this->request->param();
        $customer_id  = $data['recommend_id'];
        $pageSize      = $this->request->param('pageSize/d', 20); //每页多少条
        $pageNumber    = $this->request->param('pageNumber/d') ?? 1; //当前页

        $list = $this->model->where('recommend_id',$customer_id)->order('id','desc') ->paginate(['list_rows'=> $pageSize, 'page' => $pageNumber]);



        $this->result($list,'操作成功',1);
    }
    public function getInfo(Request $request)
    {
        $param = $request->param();
        //开始校验
        $this->validate($param, [
            'id|记录id' => 'require',
        ]);
        $id = $param['id'];
        $list = $this->model->alias('cal')->leftJoin('admin a', 'a.id = cal.action_admin_id')
            ->where('cal.id', $id)
            ->field('a.nickname,cal.*')
            ->find();
        $this->result($list, '操作成功', 1);
    }
    /**
     * @author bxm
     * @date 2022-06-20 19:03
     * 获取最近一个跟进记录
     */
    public function getInfoBycustomerId100(){
        $data = $this->request->param();
        $recommend_id  = $data['recommend_id'];
        $info = RecommendModel::where('id',$recommend_id)
            ->field('id,recommend_compay,recommend_name,recommend_mobile')->find()->toArray();



        //最近一次得跟进记录
        $recommendFollowLog = $this->model->order('id','desc')->find();
        if($recommendFollowLog){
            $date1 = new DateTime("now");

            $date2 = new DateTime($recommendFollowLog['create_time']);

            $interval = $date1->diff($date2);
            $day_diff = $interval->d;

        }

        $CustomerFollowLogInfo['day_diff'] = $day_diff ?? '';

        $info['follow_at'] = $CustomerFollowLogInfo;//

        $this->result($info,'操作成功',1);
    }

}